﻿using System;
using System.Windows.Forms;

namespace org.se.fs.view
{
    public partial class FrmBranchLists : Form, presenter.IView.Template.IFormLists
    {
        #region Public Property Variabl
        public object Lists
        {
            get { return dataGridView.DataSource; }
            set { dataGridView.DataSource = value; }
        }
        public string StatusLists
        {
            get { return groupBox.Text; }
            set { groupBox.Text = value; }
        }
        #endregion

        private readonly presenter.View.BranchLists _presenter;
        private static FrmBranchLists _frmForm;

        public FrmBranchLists()
        {
            InitializeComponent();
            _presenter = new presenter.View.BranchLists(this);
            //_presenter.OnDelete += PresenterOnDelete;
            dataGridView.AutoGenerateColumns = false;
        }

        //static void PresenterOnDelete(object source, presenter.Library.FormEvent e)
        //{
        //    MessageBox.Show(e.Exception.Message, e.IsSuccess ? "Success" : "Failed", MessageBoxButtons.OK,
        //                    e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);
        //}

        public static void FormShow(Form form)
        {
            if (_frmForm == null)
            {
                _frmForm = new FrmBranchLists {MdiParent = form};
                _frmForm.Show();
            }
            else
                _frmForm.Focus();
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnAddClick(object sender, EventArgs e)
        {
            var data = FrmBranchSubMenu.FormShow();
            _presenter.Add(data);
        }

        private void FrmFamilyBackgroundLoad(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        private void DataGridViewCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < dataGridView.Rows.Count && e.ColumnIndex < 2))
                return;

            var data = _presenter.GetData(e.RowIndex);

            if (e.ColumnIndex.Equals(0))
                _presenter.Modify(FrmBranchSubMenu.FormShow(data));
        
            else
            {
                var dialogResult = MessageBox.Show(@"Are you sure you want to delete?", @"Confirm", MessageBoxButtons.YesNo,
                                                   MessageBoxIcon.Question);
                if (dialogResult.Equals(DialogResult.No))
                    return;
                _presenter.Remove(data);
            }
        }

        private void FrmBranchListsFormClosed(object sender, FormClosedEventArgs e)
        {
            _frmForm = null;
        }
    }
}